
//sound
var sound1 = document.getElementById("sound1");
sound1.play();
var mpgJump = new Audio("audio/jump.mp3");

var canvas = document.getElementById("canvas"),
ctx = canvas.getContext("2d"),
player = new Model('img/sonic.gif', 250, 100, 29, 39),
is_space = false,
is_left = false,
is_right = false;


//score
score = 0;
//game over
gaveover = false;

//map
function initMap(map) {
	var width = 48;
	var height = 48;
	var blocks = new Array();
	for (var i=0; i<map.length; i++) {
		var item = map[i];
		var img = '';
		if (item[2] == 0) {
			img = 'img/block.png';
		} 
		if (item[2] == 1)  {
			img = 'img/coint.png';
		}
		if (item[2] == 2) {
			img = 'img/finish.png';
		}
		blocks[i] = new Model(img, width*item[0], item[1], width, height, item[2]);
	}
	return blocks;
}

var blocks = initMap(map);

//phisic
player.gravity = 20;				
player.weight = 0.2;

//events 
function keyDown(e) {
	//alert(e.keyCode);
	if (e.keyCode == 39) is_right = true; 
	if (e.keyCode == 37) is_left = true; 
	if (e.keyCode == 38) is_space = true; 
	
}

function keyUp(e) {
	if (e.keyCode == 39) is_right = false; 
	if (e.keyCode == 37) is_left = false; 
	if (e.keyCode == 38) is_space = false; 

}

//run game
MainLoop();

function MainLoop() {
	
	//move block when click left/right keys
	for (var i=0; i<blocks.length; i++) blocks[i].x += -player.velocity_x;
	//move player
	player.y += player.velocity_y;
	
	//change velocity  when click left/right keys
	if (is_right && !is_left) player.velocity_x = 5;		//if (is_right && player.velocity_y == 0) player.velocity_x = 5;
	if (is_left && !is_right) player.velocity_x = -5;		//if (is_left && player.velocity_y == 0) player.velocity_x = -5;
	
	
	//stop player
	//player.velocity_y == 0 for continue moving when in jump
	if (!is_left && !is_right && player.velocity_y == 0) player.velocity_x = 0;
	
	// velocity depend of weight
	if (player.velocity_y < player.gravity) player.velocity_y += player.weight;
	
	//check collision for Y coordinat
	//todo: need to add for X
	for (var i=0; i<blocks.length; i++) {
		//game over 
		if (player.y > 400) {
			gaveover = true;
			continue;
		}
		
		if (player.isColliding(blocks[i])) {
			//player
			if (blocks[i].type == 0){
				//blocks under to player
				if (player.y + player.height < blocks[i].y + player.velocity_y) {
					player.y = blocks[i].y - player.height;
					player.velocity_y = 0;
					continue;
				}
				
				//blocks left to player
				if (blocks[i].x < player.x + player.width + player.velocity_x &&
						//not right
						player.x < blocks[i].x + player.velocity_x &&
						//not under
						player.y + player.height > blocks[i].y && 
						//not above
						!(player.y - player.velocity_y > blocks[i].y + blocks[i].height)) {
					player.x = blocks[i].x - player.width -10;
					player.velocity_x = 0;
					player.velocity_y = player.weight;
					//alert('left player.y '+player.y+' blocks.y '+blocks[i].y+' blocks.height '+blocks[i].height+' player.velocity_y '+player.velocity_y);
					continue;
				}
				//blocks right to player
				if (blocks[i].x + blocks[i].width - player.velocity_x > player.x &&
						//not under
						player.y + player.height > blocks[i].y && 
						//not above
						!(player.y - player.velocity_y > blocks[i].y + blocks[i].height)) {
					player.x = blocks[i].x + blocks[i].width + 10;
					player.velocity_x = 0;
					player.velocity_y = player.weight;
					//alert('right player.y '+player.y+' blocks.y '+blocks[i].y+' blocks.height '+blocks[i].height+' player.velocity_y '+player.velocity_y);
					continue;
				}
				
				//blocks above to player
				if (player.velocity_y != 0 && blocks[i].y + blocks[i].height > player.y &&
						(blocks[i].x + player.velocity_x < player.x + player.width && blocks[i].x + blocks[i].width > player.x)) {
					player.y = blocks[i].y + blocks[i].height + 10;
					player.velocity_x = 0;
					player.velocity_y = player.weight;
					//alert('above blocks.x '+blocks[i].x+' player.x + width '+(player.x + player.width)+' player.velocity_y '+player.velocity_y);
					continue;
				}
			}
			
			//coints
			if (blocks[i].type == 1) {
				blocks[i].deactive();
				score = score + 1;
				continue;
			}
			
			//finish
			if (blocks[i].type == 2) {
				gaveover = true;
				continue;
			}
		}
	}
	
	//rebuild map
	for (var i=0; i<blocks.length; i++) {
		if(blocks[i].active == false) {
			blocks.splice(i, 1);
		}
	}
	
	//jump
	if (is_space && player.velocity_y == 0) {
		player.velocity_y -= 7;
		mpgJump.play();
	}
	
	//render scrin
	//clean screen each ime before render 
	ctx.clearRect(0, 0, canvas.width, canvas.height);
	for (var i=0; i<blocks.length; i++) ctx.drawImage(blocks[i].sprite, blocks[i].x, blocks[i].y);
	ctx.drawImage(player.sprite, player.x, player.y);
	if (!gaveover) {
	//score
	ctx.fillStyle = "black";
	ctx.font = "bold 16px Arial";
	ctx.fillText("Score: "+score, 300, 380);
	}
	//gaveover
	else {
		ctx.fillStyle = "black";
		ctx.font = "bold 26px Arial";
		ctx.fillText("Game Over", 300, 150);
	}
	
	if (!gaveover) {
		//timeout for game loop
		setTimeout(MainLoop, 1000/60);
	}
	
	
}

function Model(img, x, y, width, height, type) {
	this.sprite = new Image();
	this.sprite.src = img;
	// current position
	this.x = x;
	this.y = y;
	//size of model
	this.width = width;
	this.height = height;
	//velocity
	this.velocity_x = 0;
	this.velocity_y = 0;
	//gravity
	this.gravity = 0;
	this.weight = 0;
	//type of object
	this.type = type;
	
	//for delete 
	this.active = true;
	this.deactive = function() {
		this.active = false;
	}
	
	this.isColliding = function(obj) {
		if (this.x > obj.x +obj.width) return false;
		if (this.x + this.width < obj.x) return false;
		if (this.y > obj.y + obj.height) return false;
		if (this.y + this.height < obj.y) return false;
		return true;
	} 
	
}

document.body.addEventListener("keydown", function(e) {
    keyDown(e);
});

document.body.addEventListener("keyup", function(e) {
	keyUp(e);
});

sound1.addEventListener("onended", function(e) {
	sound1.play();
});